/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */
import { roleGet, userLogin } from "@/api/user";
import { UserInfoType, UserType } from "@/types/user";
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { NavigateFunction, redirect } from "react-router-dom";
import store2 from "store2";
let initialState: {
  isLogin: boolean;
  isLoading: boolean;
  userInfo: UserInfoType | null;
} = {
  isLogin: false,
  isLoading: false,
  userInfo: null,
};
// 尝试提取本地存储的数据
let uinfo =
  store2.get("react-admin-2206") || store2.session.get("react-admin-2206");
console.log("本地存储用户信息", uinfo);
if (uinfo) {
  initialState.isLogin = true;
  initialState.userInfo = uinfo;
}
const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {
    //只需要处理本地交互
    // loginStart(state, action) {
    //   state.isLoading = true;
    // },
    // loginSuccess(state, action) {
    //   state.isLoading = false;
    //   state.isLogin = true;
    //   console.log(action);
    // },
    logOut(state) {
      //退出登录
      state.isLoading = false;
      state.isLogin = false;
      state.userInfo = null;
      store2.remove("react-admin-2206");
      store2.session.remove("react-admin-2206");
    },
    //设置页更新本地用户信息
    userInfoUpdate(state, action) {
      state.userInfo = action.payload;
      //这里如何知道，用户有没有勾选自动登录
      let autoLogin = store2.get("auto-2206");
      if (autoLogin) {
        store2.set("react-admin-2206", action.payload); //本地存储
      } else {
        store2.session.set("react-admin-2206", action.payload); //会话存储
      }
    },
  },
  // 配合异步请求action
  extraReducers: (builder) => {
    builder
      .addCase(userLoginAction.pending, (state) => {
        state.isLoading = true;
      })
      .addCase(userLoginAction.fulfilled, (state, action) => {
        state.isLoading = false;
        state.isLogin = true;
        console.log("登录成功", action);
        let { info, autoLogin } = action.payload;
        state.userInfo = info; //存储用户信息
        store2.set("auto-2206", Boolean(autoLogin)); //记录用户的autoLogin
        if (autoLogin) {
          store2.set("react-admin-2206", info); //本地存储
        } else {
          store2.session.set("react-admin-2206", info); //会话存储
        }
      })
      .addCase(userLoginAction.rejected, (state) => {
        state.isLoading = false;
        state.isLogin = false;
        state.userInfo = null;
      });
  },
});

// 异步action
interface LoginType {
  values: UserType; //账号密码
  navigate: NavigateFunction; //组件传递的路由跳转方法
}
export const userLoginAction = createAsyncThunk(
  "user/loginAction",
  async (payload: LoginType) => {
    // console.log("异步action", payload);
    let res = await userLogin(payload.values);
    let roleInfo = await roleGet(res.data.roleId); //角色id获取角色信息
    // console.log("角色信息", roleInfo);

    // redirect("/category/list");
    payload.navigate("/");
    // console.log(res);

    return {
      info: {
        ...res.data,
        role: roleInfo.data,
      },
      autoLogin: payload.values.autoLogin,
    };
  }
);

export const { logOut, userInfoUpdate } = userSlice.actions;
export default userSlice.reducer;
